package com.sprint.questai.pdfTextExtraction;

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class OCRService {
    private static final Logger log = LoggerFactory.getLogger(OCRService.class);
    private final Tesseract tesseract;

    public OCRService() {
        tesseract = new Tesseract();
        //String path=System.getenv("TESSDATA_PREFIX");
        //tesseract.setDatapath("C:\\Program Files (x86)\\Tesseract-OCR\\tessdata");
        tesseract.setDatapath("src/main/resources/tessdata");
        //tesseract.setDatapath(path);
        tesseract.setLanguage("chi_sim");
    }

    public String processFile(File file) {
        try {
            return tesseract.doOCR(file);
        } catch (TesseractException e) {
            log.error("Failed to process OCR for file: {}", file.getName(), e);
            throw new RuntimeException("OCR processing failed", e);
        }
    }

//    public static void main(String[] args) {
//        OCRService ocrService = new OCRService();
//
//        // 测试文件路径
//        String filePath = "D:\\Project\\QuestAi\\quest-ai-backend\\src\\main\\resources\\legal_data\\PDF\\指导性案例229号：沙某某诉袁某某探望权纠纷案(FBM-CLI.C.551665662).pdf";
//
//        File file = new File(filePath);
//        if (!file.exists()) {
//            System.err.println("Test file not found: " + filePath);
//            return;
//        }
//
//        try {
//            String result = ocrService.processFile(file);
//            System.out.println("OCR Result: " + result);
//        } catch (RuntimeException e) {
//            System.err.println("Failed to process OCR: " + e.getMessage());
//        }
//    }
}
